<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Sch_desc extends Model
{
    //
	protected $table='sch_desc';
	protected $fillable=['id','scheduling_id','station_id','station_name','store_id',
	'staff_id','time','start_time','end_time','sign_start','sign_end','is_settle','settle_id','start_time','end_time','staff_task_id','company_id'
	];
	protected $hidden=[];
	public function create_sch_desc($id,$data,$store_id,$company_id){
		foreach($data['time'] as $kk=>$vv){
			foreach($data['desc'] as $k=>$v){
				$newArr=[];
				$newArr['scheduling_id']=$id;
				$newArr['store_id']=$store_id;
				$newArr['station_id']=$v['station_id'];
				$newArr['staff_id']=$v['staff_id'];
				$newArr['time']=$vv;
				$newArr['staff_task_id']=$v['staff_task_id'];
				$newArr['company_id']=$company_id;
				foreach($v['sch'] as $key=>$value){
					$newArr['start_time']=$value['start_time'];
					$newArr['end_time']=$value['end_time'];
				}
				$this->updateOrCreate(['time'=>$newArr['time'],'staff_id'=>$newArr['staff_id'],
					'company_id'=>$newArr['company_id'],'start_time'=>$newArr['start_time'],
					'end_time'=>$newArr['end_time']],$newArr);
			}
		}
	}



//	查询签到情况
	public function sign_desc($staff_id){
		$allsign=$this->where(['staff_id'=>$staff_id])->count();
		$ready_sign=$this->where(['staff_id'=>$staff_id])->where('sign_start','!=',null)
			->where('sign_end','!=',null)->count();
		return $allsign.'/'.$ready_sign;
	}


//	生成工单详情
	public function crate_settle($parameter){
		 $data=$this->where(function ($query) use($parameter) {
//			$query->whereBetween('time',[$parameter['start_time'],$parameter['end_time']]);
			$query->whereIn('time',$parameter['settle_date']);
			$query->where(['store_id'=>$parameter['store_id'],'is_settle'=>null]);
		})->with('staff_info')->with('station')->get();
		foreach($data as $kk=>$vv){
			$data[$kk]['staff_name']=$vv['staff_info']['name'];
			$data[$kk]['station_name']=$vv['station']['name'];
			$data[$kk]['should_hour']=(strtotime('1993-07-05'.$vv['end_time'])-strtotime('1993-07-05'.$vv['start_time']))/3600;
			$data[$kk]['actual_hour']=(strtotime('1993-07-05'.$vv['sign_end'])-strtotime('1993-07-05'.$vv['sign_start']))/3600;
			$data[$kk]['hour_salary']=$vv['station']['hour_salary'];
		}

		$newArr=[];
		foreach($data as $k=>$v){
			$newArr[$v['staff_id']][]=$v;
		}
//		return $newArr;
		foreach($newArr as $i=>$j){
			$newArr[$i]['sum_should_hour']=0;
			$newArr[$i]['sum_actual_hour']=0;
			foreach($j as $m=>$n){
				$newArr[$i]['station_id']=$n['station']['id'];
				$newArr[$i]['station_name']=$n['station']['name'];
				$newArr[$i]['staff_id']=$n['staff_info']['id'];
				$newArr[$i]['staff_name']=$n['staff_info']['name'];
				$newArr[$i]['hour_salary']=$n['station']['hour_salary'];
				$newArr[$i]['sum_should_hour']+=$n['should_hour'];
				$newArr[$i]['sum_actual_hour']+=$n['actual_hour'];
			}
		}
		$str=[];
		foreach($newArr as $key=>$value){
			$str[$key]['staff_id']=$value['staff_id'];
			$str[$key]['store_station_id']=$value['station_id'];
			$str[$key]['staff_name']=$value['staff_name'];
			$str[$key]['station_name']=$value['station_name'];
			$str[$key]['should_hour']=$value['sum_should_hour'];
			$str[$key]['actual_hour']=$value['sum_actual_hour'];
			$str[$key]['hour_salary']=$value['hour_salary'];
			$str[$key]['settle_id']=$parameter['settle_id'];
			$str[$key]['store_id']=$parameter['store_id'];
			$str[$key]['nonstandard_hour']=$value['sum_actual_hour']-$value['sum_should_hour'];
			$str[$key]['start_time']=date('Y-m',strtotime($parameter['start_time']));
			$str[$key]['end_time']=date('Y-m',strtotime($parameter['end_time']));
			$str[$key]['created_at']=date("Y-m-d H:i:s",time());
		}
		$str=array_values($str);
//		return $str;
//		添加工单详情
		DB::beginTransaction();
		try{
			DB::table('settle_desc')->insert($str);
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}

		if(!isset($message)){
			return true;
		}else{
			return $exception;
		}
	}


//	员工信息
	public function staff_info(){
		return $this->belongsTo('App\Model\Staff','staff_id');
	}

//	岗位信息
	public function station(){
		return $this->belongsTo('App\Model\Station','station_id');
	}


	public function alltime($id,$start_time,$end_time,$staff_id){
		$data['Scheduling_time']=$this->where(function ($query) use($start_time,$end_time,$staff_id){
			$query->where(['staff_id'=>$staff_id]);
			$query->whereBetween('time',[$start_time,$end_time]);
		})->pluck('time');
		$data['sign']=$this->where(function ($query) use($start_time,$end_time,$staff_id){
			$query->where(['staff_id'=>$staff_id]);
			$query->whereBetween('time',[$start_time,$end_time]);
			$query->whereNotNull('sign_start');
			$query->whereNotNull('sign_end');
		})->pluck('time');

		$data['no_sign']=$this->where(function ($query) use($start_time,$end_time,$staff_id){
			$query->where(['staff_id'=>$staff_id]);
			$query->whereBetween('time',[$start_time,$end_time]);
			$query->whereNull('sign_start');
			$query->whereNull('sign_end');
		})->pluck('time');
		return $data;
	}

	//店长端获取排班列表
	public function store_sch_lists($where){
		$data= $this->where(['store_id'=>$where['store_id']])->where(function ($query) use($where){
			$query->whereBetween('time',[$where['start_time'],$where['end_time']]);
		})->get();
		if(count($data)){
			foreach($data as $kk=>$vv){
				$newArr[]=$vv['time'];
			}
		}else{
			$newArr=[];
		}

		return array_unique($newArr);

	}

	//店长端获取排班详情
	public function store_sch_desc($where){
		return $this->where(['store_id'=>$where['store_id']])
			->with('staff_info')->with('station')->where(['time'=>$where['time']])->get();
	}

//	查询所有的签到记录
	public function allSign($where){
		$arr=$this->where(['store_id'=>$where['store_id']])->where(function ($query) use($where){
			if($where['store_station_id'] !=null){
				$query->where(['station_id'=>$where['store_station_id']]);
			}
		});
		$allData=$arr->where(function ($query) use($where){
				$query->whereBetween('time',[$where['start_time'],$where['end_time']]);
		})->with('station')->get();

		$signData=$arr->where(function ($query) use($where){
			$query->whereBetween('time',[$where['start_time'],$where['end_time']]);
				$query->whereNotNull('sign_start')->whereNotNull('sign_end');
		})->with('station')->get();
		if(count($allData)==0 || count($signData)==0){
			return 0 ;
		}
		$newArr=[];
		$sum_hour_all=0;$sum_hour_sign=0;
		foreach($allData as $kk=>$vv){
			$sum_hour_all +=(strtotime($vv['time'].$vv['end_time'])-strtotime($vv['time'].$vv['start_time']))/3600;
		}
		foreach($signData as $kk=>$vv){
			$sum_hour_sign +=(strtotime($vv['time'].$vv['sign_end'])-strtotime($vv['time'].$vv['sign_start']))/3600;
		}
		return  round($sum_hour_sign/$sum_hour_all,2);
	}

//	查询排班详情
	public function desc($scheduling_id,$time){
		$data=$this->where(['scheduling_id'=>$scheduling_id,'time'=>$time])
			->with('staff_info')->with('station')->get();
		return $data;
	}



//	查询编辑日期和员工
	public function staff_date($scheduling_id){
		$data=$this->where(['scheduling_id'=>$scheduling_id])->with('staff_info')->with('station')->get();
		$newArr=[];
		foreach($data as $kk=>$vv){
			$newArr[$vv['staff_id']]['staff_id']=$vv['staff_id'];
			$newArr[$vv['staff_id']]['station_id']=$vv['station_id'];
			$newArr[$vv['staff_id']]['staff_task_id']=$vv['staff_task_id'];
			$newArr[$vv['staff_id']]['staff_name']=$vv['staff_info']['name'];
			$newArr[$vv['staff_id']]['station_name']=$vv['station']['name'];
		}
		$array=[];
		foreach($newArr as $kk=>$vv){
			$array[]=$vv;
		}
		return $array;
	}


//	查询改员工任务的已签到情况
	public function sign_staff_task($staff_task_id){
		$data=$this->where(['staff_task_id'=>$staff_task_id])
			->whereNotNull('sign_start')->pluck('time');
		return $data;
	}
//	查询改员工任务的未签到情况
	public function no_sign_staff_task($staff_task_id){
		$data=$this->where(['staff_task_id'=>$staff_task_id])
			->whereNull('sign_start')
			->pluck('time');
		return $data;
	}

//	查询员工任务的排班情况
	public function sch_day($staff_task_id){
		$data=$this->where(['staff_task_id'=>$staff_task_id])->pluck('time');
		return $data;
	}

}
